{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Learning Optimization\n",
    "\n",
    "This is a step-by-step example of how to use the Classiq platform at the application level. The goal is to see how easy it can be to use quantum algorithms to solve problems.\n",
    "\n",
    "This is a basic optimization problem:\n",
    "\n",
    "*minimize the expression* $3x_1+2x_2$ *for the non-negative integers* $x_1,x_2$, *given the constraint that* $3x_1+x_2\\ge2$.\n",
    "\n",
    "Of course this is trivial and the solution is $x_1=1,x_2=0$. The goal is to understand how to incorporate the problem with the platform, so you can then continue on to define more complicated problems.\n",
    "\n",
    "This kind of optimization problem is relevant for many real-life scenarios. For example, Amazon wants to determine the best value it can offer to a customer for two items on a Black Friday sale, but it has to earn some minimum value.\n",
    "\n",
    "## How to Solve It?\n",
    "\n",
    "Define the optimization problem with the classical [Pyomo](https://pyomo.readthedocs.io/en/stable/index.html) optimization package in Python. Then, use the platform to convert it to a high-level functional model of quantum algorithms. This functional model is at the heart of the platform as this is the object that is synthesized to an actual quantum circuit using the synthesis engine!\n",
    "\n",
    "After the circuit is synthesized, run it on actual hardware or on a simulator to actually get the result from the quantum algorithm. This tutorial runs the algorithm on the IBM quantum simulator as it is the default option.\n",
    "\n",
    "## What Do You Need to Know about Quantum Algorithms?\n",
    "\n",
    "You need to know almost nothing regarding quantum algorithms, besides one thing. There are two common algorithms used for optimization problems (as well as chemistry): QAOA and VQE. Both are very similar, where QAOA could be seen as a specific type of VQE.\n",
    "\n",
    "For this problem, use the QAOA algorithm. The algorithm has a mandatory parameter that you need to choose, as explained below.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting Started\n",
    "Import the relevant packages. The first is the Pyomo package; the classical optimization package that was installed when you installed Classiq:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:20.699750Z",
     "iopub.status.busy": "2024-05-07T14:28:20.699315Z",
     "iopub.status.idle": "2024-05-07T14:28:20.967942Z",
     "shell.execute_reply": "2024-05-07T14:28:20.967312Z"
    }
   },
   "outputs": [],
   "source": [
    "import pyomo.environ as pyo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import the objects that translate the optimization problem from the Pyomo language to a high-level quantum functional model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:20.971264Z",
     "iopub.status.busy": "2024-05-07T14:28:20.970551Z",
     "iopub.status.idle": "2024-05-07T14:28:23.813366Z",
     "shell.execute_reply": "2024-05-07T14:28:23.812502Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import construct_combinatorial_optimization_model\n",
    "from classiq.applications.combinatorial_optimization import OptimizerConfig, QAOAConfig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Defining the Problem\n",
    "\n",
    "Initiate a Pyomo application object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:23.818516Z",
     "iopub.status.busy": "2024-05-07T14:28:23.817092Z",
     "iopub.status.idle": "2024-05-07T14:28:23.822477Z",
     "shell.execute_reply": "2024-05-07T14:28:23.821760Z"
    }
   },
   "outputs": [],
   "source": [
    "application_level_object = pyo.ConcreteModel()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " This object will contain all the relevant information regarding the optimization problem. The first piece of relevant information is what are the variables. In Pyomo, the way to incorporate the information regarding the variables is using the `pyo.Var` object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:23.827310Z",
     "iopub.status.busy": "2024-05-07T14:28:23.826052Z",
     "iopub.status.idle": "2024-05-07T14:28:23.831891Z",
     "shell.execute_reply": "2024-05-07T14:28:23.831185Z"
    }
   },
   "outputs": [],
   "source": [
    "application_level_object.x = pyo.Var(\n",
    "    [1, 2],  # variables names\n",
    "    domain=pyo.NonNegativeIntegers,  # variables type\n",
    "    bounds=(0, 3),  # variables range\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the first line, define 'application_object' with a field called 'x' to contain the problem variables. The variables are defined with a 'pyo.Var' object, containing several things:\n",
    "\n",
    "1. The names of the variables.\n",
    "These are defined by $[1,2]$, indicating $x_1$ and $x_2$, respectively. (Likewise, $[3,7]$ would indicate two variables: $x_3, x_7$.)\n",
    "\n",
    "2. The type/domain of the variables.\n",
    "The variables are non-negative integers, so configure them accordingly using the 'pyo.NonNegativeIntegers' command.\n",
    "\n",
    "3. The bounds of the variables.\n",
    "The variables are configured to get values from 0 to 3, inclusive.\n",
    "\n",
    "While you may prefer a larger range, today's quantum computers (and simulators) are not big enough, so the size of the problems you can solve is quite small (here defined by the number of options: two variables each with four options; i.e., 16 options total).\n",
    "\n",
    "In the application object, define the cost function, which is the objective:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:23.836925Z",
     "iopub.status.busy": "2024-05-07T14:28:23.835055Z",
     "iopub.status.idle": "2024-05-07T14:28:23.842463Z",
     "shell.execute_reply": "2024-05-07T14:28:23.841556Z"
    }
   },
   "outputs": [],
   "source": [
    "application_level_object.cost = pyo.Objective(\n",
    "    expr=3 * application_level_object.x[1] + 2 * application_level_object.x[2]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In other words, *minimize* $3x_1+2x_2$. Together with the objective, define the constraint:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:23.846121Z",
     "iopub.status.busy": "2024-05-07T14:28:23.845617Z",
     "iopub.status.idle": "2024-05-07T14:28:23.849143Z",
     "shell.execute_reply": "2024-05-07T14:28:23.848570Z"
    }
   },
   "outputs": [],
   "source": [
    "application_level_object.constraint = pyo.Constraint(\n",
    "    expr=3 * application_level_object.x[1] + application_level_object.x[2] >= 2\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I.e., the constraint is $3x_1 + 2x_2 \\ge 2$. There are several ways to define constraints in Pyomo. Here, add a field to the application object called `constraint`, which is equal to some Pyomo constraint object. Read [link](https://docs.classiq.io/latest/reference-manual/built-in-algorithms/combinatorial-optimization/problem-formulation/) for more ways of defining constraints.\n",
    "\n",
    "Examine the application object using the Pyomo method 'pprint':\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:23.851338Z",
     "iopub.status.busy": "2024-05-07T14:28:23.851047Z",
     "iopub.status.idle": "2024-05-07T14:28:23.857104Z",
     "shell.execute_reply": "2024-05-07T14:28:23.856498Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 Set Declarations\n",
      "    x_index : Size=1, Index=None, Ordered=Insertion\n",
      "        Key  : Dimen : Domain : Size : Members\n",
      "        None :     1 :    Any :    2 : {1, 2}\n",
      "\n",
      "1 Var Declarations\n",
      "    x : Size=2, Index=x_index\n",
      "        Key : Lower : Value : Upper : Fixed : Stale : Domain\n",
      "          1 :     0 :  None :     3 : False :  True : NonNegativeIntegers\n",
      "          2 :     0 :  None :     3 : False :  True : NonNegativeIntegers\n",
      "\n",
      "1 Objective Declarations\n",
      "    cost : Size=1, Index=None, Active=True\n",
      "        Key  : Active : Sense    : Expression\n",
      "        None :   True : minimize : 3*x[1] + 2*x[2]\n",
      "\n",
      "1 Constraint Declarations\n",
      "    constraint : Size=1, Index=None, Active=True\n",
      "        Key  : Lower : Body          : Upper : Active\n",
      "        None :   2.0 : 3*x[1] + x[2] :  +Inf :   True\n",
      "\n",
      "4 Declarations: x_index x cost constraint\n"
     ]
    }
   ],
   "source": [
    "application_level_object.pprint()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See how all the information regarding the problem is organized in this Pyomo application object.\n",
    "\n",
    "## Entering the Quantum World\n",
    "\n",
    "So far, you have only used the Pyomo package and spoken the *optimization language*. You now need some quantum knowledge for the optimization problem.\n",
    "\n",
    "Set the number of repetitions of the QAOA sub-circuit. The QAOA algorithm contains a QAOA sub-circuit that might repeat several times. Roughly speaking, the more repetitions, the better the algorithm.\n",
    "\n",
    "Having said that, as you saw, there is a limited range of parameters due to the small size of today's quantum computers. There is also an issue with the length of the quantum circuit due to the relatively low quality of today's quantum circuits (again due to the limited power of the quantum simulators). Therefore, start with one repetition of the sub-circuit, and later you can change it to see how the results change.\n",
    "\n",
    "Define the QAOA configuration:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:23.892812Z",
     "iopub.status.busy": "2024-05-07T14:28:23.892239Z",
     "iopub.status.idle": "2024-05-07T14:28:23.895652Z",
     "shell.execute_reply": "2024-05-07T14:28:23.895034Z"
    }
   },
   "outputs": [],
   "source": [
    "qaoa_config = QAOAConfig(num_layers=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Seamlessly Generating the Functional Level Model\n",
    "Now with the application object and the QAOA configuration defined, ask the platform to convert it into a high-level quantum functional model. Because there is an optimization problem, use `construct_combinatorial_optimization_model`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:23.898169Z",
     "iopub.status.busy": "2024-05-07T14:28:23.897717Z",
     "iopub.status.idle": "2024-05-07T14:28:24.276178Z",
     "shell.execute_reply": "2024-05-07T14:28:24.275462Z"
    }
   },
   "outputs": [],
   "source": [
    "model = construct_combinatorial_optimization_model(\n",
    "    pyo_model=application_level_object,\n",
    "    qaoa_config=qaoa_config,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Congratulations! You just defined your first quantum model that encapsulates the functionality of your quantum algorithm, without mentioning anything related to qubits or quantum gates!\n",
    "\n",
    "Ask the system to solve the model using the quantum algorithm:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Synthesizing the model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:24.279195Z",
     "iopub.status.busy": "2024-05-07T14:28:24.278837Z",
     "iopub.status.idle": "2024-05-07T14:28:26.800358Z",
     "shell.execute_reply": "2024-05-07T14:28:26.799605Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import show, synthesize\n",
    "\n",
    "qprog = synthesize(model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Executing:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:26.805103Z",
     "iopub.status.busy": "2024-05-07T14:28:26.803878Z",
     "iopub.status.idle": "2024-05-07T14:28:30.378028Z",
     "shell.execute_reply": "2024-05-07T14:28:30.377258Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import execute\n",
    "\n",
    "res = execute(qprog).result()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Examine the solution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:30.382086Z",
     "iopub.status.busy": "2024-05-07T14:28:30.381816Z",
     "iopub.status.idle": "2024-05-07T14:28:30.638617Z",
     "shell.execute_reply": "2024-05-07T14:28:30.635286Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>probability</th>\n",
       "      <th>cost</th>\n",
       "      <th>solution</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0.008</td>\n",
       "      <td>3.0</td>\n",
       "      <td>[1, 0]</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>115</th>\n",
       "      <td>0.002</td>\n",
       "      <td>4.0</td>\n",
       "      <td>[0, 1]</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>187</th>\n",
       "      <td>0.001</td>\n",
       "      <td>4.0</td>\n",
       "      <td>[0, 2]</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>120</th>\n",
       "      <td>0.002</td>\n",
       "      <td>5.0</td>\n",
       "      <td>[1, 0]</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>0.002</td>\n",
       "      <td>5.0</td>\n",
       "      <td>[1, 0]</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     probability  cost solution  count\n",
       "34         0.008   3.0   [1, 0]      8\n",
       "115        0.002   4.0   [0, 1]      2\n",
       "187        0.001   4.0   [0, 2]      1\n",
       "120        0.002   5.0   [1, 0]      2\n",
       "136        0.002   5.0   [1, 0]      2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from classiq.applications.combinatorial_optimization import (\n",
    "    get_optimization_solution_from_pyo,\n",
    ")\n",
    "\n",
    "solution = get_optimization_solution_from_pyo(\n",
    "    application_level_object,\n",
    "    vqe_result=res[0].value,\n",
    "    penalty_energy=qaoa_config.penalty_energy,\n",
    ")\n",
    "optimization_result = pd.DataFrame.from_records(solution)\n",
    "optimization_result.sort_values(by=\"cost\", ascending=True).head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:30.642492Z",
     "iopub.status.busy": "2024-05-07T14:28:30.642211Z",
     "iopub.status.idle": "2024-05-07T14:28:30.655667Z",
     "shell.execute_reply": "2024-05-07T14:28:30.652658Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = [1, 0] , cost = 2.9999999999999964\n"
     ]
    }
   ],
   "source": [
    "idx = optimization_result.cost.idxmin()\n",
    "print(\n",
    "    \"x =\", optimization_result.solution[idx], \", cost =\", optimization_result.cost[idx]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The solution is $x_1=1, x_2=0$. Yes, you succeeded in solving the optimization problem using a quantum algorithm! :)\n",
    "\n",
    "Wait a minute, you might ask, *'Where are all the qubits and gates I have heard about?\n",
    "'*\n",
    "\n",
    "That is a good question! While you can design algorithms at the application and functional levels, you also have access to the qubit level to further understand the algorithm and get into detail with more options!\n",
    "\n",
    "This is easily done. The synthesis engine output is a quantum circuit object, so visualize it with the 'show' command that prompts a website to interactively display the circuit for deeper analysis. You can examine how your circuit looks, from high level to the qubit level."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:30.659628Z",
     "iopub.status.busy": "2024-05-07T14:28:30.659333Z",
     "iopub.status.idle": "2024-05-07T14:28:30.772641Z",
     "shell.execute_reply": "2024-05-07T14:28:30.771953Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/16c77e9c-2adc-4173-b8b0-9f4655339242?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "show(qprog)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that you understand better how the platform works, there is one last thing worth mentioning. When you solved the model by executing the synthesis engine's output quantum circuit, behind the scenes the circuit was sent to the default Executor (IBM simulator) with classical optimization preferences to return the optimization result.\n",
    "\n",
    "Because this example shows how to use the platform at the application level, many details in the flow were determined behind the scenes. For more control of your design, and to design the algorithm from the functional level so to gain more control and capabilities, do the next tutorial ;)\n",
    "\n",
    "For your convenience, all the code is provided in the following block:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:28:30.775304Z",
     "iopub.status.busy": "2024-05-07T14:28:30.774826Z",
     "iopub.status.idle": "2024-05-07T14:28:35.890791Z",
     "shell.execute_reply": "2024-05-07T14:28:35.889973Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 Set Declarations\n",
      "    x_index : Size=1, Index=None, Ordered=Insertion\n",
      "        Key  : Dimen : Domain : Size : Members\n",
      "        None :     1 :    Any :    2 : {1, 2}\n",
      "\n",
      "1 Var Declarations\n",
      "    x : Size=2, Index=x_index\n",
      "        Key : Lower : Value : Upper : Fixed : Stale : Domain\n",
      "          1 :     0 :  None :     3 : False :  True : NonNegativeIntegers\n",
      "          2 :     0 :  None :     3 : False :  True : NonNegativeIntegers\n",
      "\n",
      "1 Objective Declarations\n",
      "    cost : Size=1, Index=None, Active=True\n",
      "        Key  : Active : Sense    : Expression\n",
      "        None :   True : minimize : 3*x[1] + 2*x[2]\n",
      "\n",
      "1 Constraint Declarations\n",
      "    constraint : Size=1, Index=None, Active=True\n",
      "        Key  : Lower : Body          : Upper : Active\n",
      "        None :   2.0 : 3*x[1] + x[2] :  +Inf :   True\n",
      "\n",
      "4 Declarations: x_index x cost constraint\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = [1, 0] , cost = 2.9999999999999964\n",
      "Opening: https://platform.classiq.io/circuit/c5c1b8d5-6c30-419c-a238-ad5d40b037d3?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "# This tutorial was tested on Classiq version 0.19\n",
    "\n",
    "import pandas as pd\n",
    "import pyomo.environ as pyo\n",
    "\n",
    "from classiq import (\n",
    "    construct_combinatorial_optimization_model,\n",
    "    execute,\n",
    "    show,\n",
    "    synthesize,\n",
    ")\n",
    "from classiq.applications.combinatorial_optimization import OptimizerConfig, QAOAConfig\n",
    "\n",
    "# Application object definitions and fields\n",
    "application_object = pyo.ConcreteModel()\n",
    "\n",
    "application_object.x = pyo.Var(\n",
    "    [1, 2],  # variables names\n",
    "    domain=pyo.NonNegativeIntegers,  # variables type\n",
    "    bounds=(0, 3),  # variables range\n",
    ")\n",
    "\n",
    "application_object.cost = pyo.Objective(\n",
    "    expr=3 * application_object.x[1] + 2 * application_object.x[2]\n",
    ")\n",
    "\n",
    "application_object.constraint = pyo.Constraint(\n",
    "    expr=3 * application_object.x[1] + application_object.x[2] >= 2\n",
    ")\n",
    "\n",
    "application_object.pprint()\n",
    "\n",
    "# going quantum - QAOA preferences\n",
    "qaoa_config = QAOAConfig(num_layers=1)  # QAOA sub-circuit number of repetitions\n",
    "\n",
    "# defining the model\n",
    "model = construct_combinatorial_optimization_model(\n",
    "    pyo_model=application_level_object,\n",
    "    qaoa_config=qaoa_config,\n",
    ")\n",
    "\n",
    "# synthesizing a quantum circuit\n",
    "qprog = synthesize(model)\n",
    "\n",
    "# executing the circuit to solve the optimzation problem:\n",
    "res = execute(qprog).result()\n",
    "\n",
    "# post-processing the results:\n",
    "from classiq.applications.combinatorial_optimization import (\n",
    "    get_optimization_solution_from_pyo,\n",
    ")\n",
    "\n",
    "solution = get_optimization_solution_from_pyo(\n",
    "    application_level_object,\n",
    "    vqe_result=res[0].value,\n",
    "    penalty_energy=qaoa_config.penalty_energy,\n",
    ")\n",
    "optimization_result = pd.DataFrame.from_records(solution)\n",
    "optimization_result.sort_values(by=\"cost\", ascending=True).head(5)\n",
    "idx = optimization_result.cost.idxmin()\n",
    "print(\n",
    "    \"x =\", optimization_result.solution[idx], \", cost =\", optimization_result.cost[idx]\n",
    ")\n",
    "\n",
    "# view and analyze the quantum circuit:\n",
    "show(qprog)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "ec402a1632ecaf364e9e142e2f48c85b41212f15ad2d88f302a959b5a6769b70"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
